package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> list(String name, String label, Integer page, Integer pageSize) {
        //1.设置分页查询
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接口方法 执行查询
        List<Role> list = roleMapper.listAll(name, label);
        //3.将查询结果(强转list-->page)封装成PageResult对象并返回
        Page<Role> p = (Page<Role>) list;//为什么要强转,list中没有getTotal和getResult方法,但是page中有
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void add(Role role) {
        //完善数据格式
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Role get(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public Object listAll2() {
        return roleMapper.listAll3();

    }

}
